package com.joey.color_test;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.holly.common.utils.L;
import com.holly.common_view.listener.OnItemClickListener;

/**
 * 多选框
 * <p>
 * 2016/3/26 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public class ColorChangeDialog extends Dialog {
    private final String TAG = L.createTag(this);
    private EditText mEditColor;
    private View mViewColor;
    private SeekBar mSeekBar1;
    private SeekBar mSeekBar2;
    private SeekBar mSeekBar3;
    private TextView mTextViewOk;

    private OnItemClickListener<Integer> mOnColorChangeListener;
    private final SeekBarListener mSeekBarListener;
    private final TextChangeListener mTextChangeListener;

    public void setOnColorChangeListener(OnItemClickListener <Integer>onColorChangeListener) {
        mOnColorChangeListener = onColorChangeListener;
    }

    public ColorChangeDialog(@NonNull Context context) {
        super(context, R.style.DialogStyle);
        //点击屏幕外关闭
        setCanceledOnTouchOutside(true);

        Context themeContext = getContext();
        //设置为底部
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        View view = LayoutInflater.from(themeContext).inflate(R.layout.dialog_color_change, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        setContentView(view, layoutParams);
        mEditColor = (EditText) view.findViewById(R.id.edit_color);
        mViewColor = view.findViewById(R.id.view_color);
        mSeekBar1 = (SeekBar) view.findViewById(R.id.seek_bar_1);
        mSeekBar2 = (SeekBar) view.findViewById(R.id.seek_bar_2);
        mSeekBar3 = (SeekBar) view.findViewById(R.id.seek_bar_3);
        mTextViewOk = (TextView) view.findViewById(R.id.text_view_ok);

        mSeekBarListener = new SeekBarListener();
        mSeekBar1.setOnSeekBarChangeListener(mSeekBarListener);
        mSeekBar2.setOnSeekBarChangeListener(mSeekBarListener);
        mSeekBar3.setOnSeekBarChangeListener(mSeekBarListener);
        mTextChangeListener = new TextChangeListener();
        mEditColor.addTextChangedListener(mTextChangeListener);

        mTextViewOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    int color = Color.parseColor("#" + mEditColor.getText().toString());
                    if (mOnColorChangeListener != null) {
                        mOnColorChangeListener.onItemClick(color);
                    }
                    dismiss();
                } catch (Exception e) {
                    Toast.makeText(getContext(),"颜色格式有误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setColor(int color) {
        String colorStr = String.format("%06x", color).toUpperCase();
        mEditColor.setText(colorStr);
    }

    @Override
    public void show() {
        //设置为底部
        Window window = getWindow();
        WindowManager wm = (WindowManager) getContext()
                .getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width * 8 / 10;
        lp.height = height * 3 / 10;
        window.setAttributes(lp);
        super.show();
    }

    private class SeekBarListener implements SeekBar.OnSeekBarChangeListener {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int color = (mSeekBar1.getProgress()<<16) + (mSeekBar2.getProgress()<<8)+mSeekBar3.getProgress();
            mEditColor.removeTextChangedListener(mTextChangeListener);
            mEditColor.setText(String.format("%06x", color).toUpperCase());
            mEditColor.setSelection(mEditColor.getText().length());
            mViewColor.setBackgroundColor(Color.parseColor("#"+mEditColor.getText().toString()));
            mEditColor.addTextChangedListener(mTextChangeListener);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    }

    private class TextChangeListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() > 0) {
                try {
                    String text = mEditColor.getText().toString();
                    int color = Color.parseColor("#" + text);
                    int color1 = (color>>16)&(255);
                    int color2 = (color>>8)&(255);
                    int color3 = color&(255);
                    mSeekBar1.setOnSeekBarChangeListener(null);
                    mSeekBar2.setOnSeekBarChangeListener(null);
                    mSeekBar3.setOnSeekBarChangeListener(null);
                    mSeekBar1.setProgress(color1);
                    mSeekBar2.setProgress(color2);
                    mSeekBar3.setProgress(color3);
                    mSeekBar1.setOnSeekBarChangeListener(mSeekBarListener);
                    mSeekBar2.setOnSeekBarChangeListener(mSeekBarListener);
                    mSeekBar3.setOnSeekBarChangeListener(mSeekBarListener);
                    mViewColor.setBackgroundColor(color);
                } catch (Exception e) {
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
